package com.laboratory.report.lab05.lab0501;

import lombok.Getter;
import lombok.SneakyThrows;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 测试
 */
public class Lab0501Main {

    @SneakyThrows
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(2);

        Athlete athlete1 = new Athlete("张飞");
        Athlete athlete2 = new Athlete("曹操");
        es.submit(athlete1).get();
        es.submit(athlete2).get();
        System.out.println((athlete1.getTime()>athlete2.getTime()?athlete2.getAthName():athlete1.getAthName()) + "获胜");
    }

    @Getter
    public static class Athlete extends Thread {

        private String athName;
        private double distance = 500;
        // 随机产生40~50千米/小时的速度，并转换成米/秒
        private double rapid = (40+Math.random()*10)*1000/3600;
        private int time;

        public Athlete(String athName) {
            this.athName = athName;
        }

        @SneakyThrows
        @Override
        public void run() {
            while(distance>0) {
                distance -= rapid;
                ++time;
            }
            System.out.println("运动员"+athName+"已到达终点，共耗时:"+time+"秒");
        }
    }
}
